/*
============================================================================
 Name        : BlockContacterList.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CBlockContacterList implementation
============================================================================
*/

#include "BlockContacterList.h"
#include <cntdb.h> 
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include "BlockRecommend.h"
#include "eStockappui.h"
#include <eStock_L2hy.rsg>

const TInt KMenuIdRecmd = 100;	//\x{fffd}\x{01bc}\x{fffd}\x{fffd}\x{00fb}\x{fffd}
const TInt KMenuIdSelAll = 101;		//\x{022b}\x{0461}
const TInt KMenuIdSelNone = 102;	//\x{022b}\x{fffd}\x{fffd}\x{0221}\x{fffd}\x{fffd}

CBlockContacterList::CBlockContacterList(CBaseBlock* bsBack):
CBlock(bsBack)
{
}

CBlockContacterList::~CBlockContacterList()
{
	m_vContacters.Close();
}

CBlockContacterList* CBlockContacterList::NewLC(CBaseBlock* bsBack)
{
	CBlockContacterList* self = new (ELeave)CBlockContacterList(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockContacterList* CBlockContacterList::NewL(CBaseBlock* bsBack)
{
	CBlockContacterList* self=CBlockContacterList::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockContacterList::ConstructL()
{
	CBlock::ConstructL();
	InitCntList();
	InitPopMenu(0);
	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_OPTION);
	CGlobal::GetDesFromID(m_strTitle, LOC_BUF_STR_SELCONTACTER);
}

void CBlockContacterList::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockContacterList::OnDraw(CFbsBitGc& g)const
{
	g.UseFont(m_pGlobal->g_font);
	g.SetPenColor(m_pGlobal->g_rgbText);
	TBuf<64> temp;
	if(m_vContacters.Count() < 1)
	{
		CGlobal::GetDesFromID(temp, LOC_BUF_STR_NOCONTACTER);
		g.DrawText(temp, TPoint(0, m_rect.iTl.iY+m_pGlobal->g_nFontHeight));
		return;
	}

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_CHECKMARKER);
	TInt nStart = m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;
	TInt nX = 5;
	TInt n = m_nLineHeight/2;
	TInt tx = nX+(n-m_pGlobal->g_font->TextWidthInPixels(temp))/2;


	for (TInt i=0; i<m_nLinesPerPage; i++)
	{
		TInt nPos = nStart+i;
		if (nPos>=m_vContacters.Count())
			break;

		TContacter contacter = m_vContacters[nPos];

		TInt nY = m_rect.iTl.iY + m_nLineHeight*i;

		if (m_nCurrent==nPos)
		{
			//g.SetBrushColor(m_pGlobal->g_rgbHL_BG);
			//g.DrawRect(TRect(TPoint(0, nY), TSize(m_rect.iBr.iX, m_nLineHeight)));
			//CGlobal::Draw3DRect(g,TRect(TPoint(0, nY), TSize(m_rect.iBr.iX, m_nLineHeight)),EFalse);
			DrawImageRect(g,0, nY, m_rect.Width(), nY+m_nLineHeight,1);
		}

		g.SetPenColor(m_pGlobal->g_rgbText);
		g.SetBrushColor(m_pGlobal->g_rgbBackGround);
		
		TInt y = nY+n/2;
		g.DrawRect(TRect(TPoint(nX, y), TSize(n, n)));

		if (contacter.iSelected)
		{
			TInt ty = y+(n-m_pGlobal->g_nFontHeight)/2;
			g.DrawText(temp, TPoint(tx, ty+m_pGlobal->g_nFontHeight));
		}

		g.SetPenColor(m_pGlobal->g_rgbText);
		g.DrawText(contacter.iName, TPoint(nX+n+5, nY+m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));
		TInt width = m_pGlobal->g_font->TextWidthInPixels(contacter.iPhone);
		g.DrawText(contacter.iPhone,TPoint(m_rect.iBr.iX-width, nY+m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));
	}
}

void CBlockContacterList::OnKeyDown(TInt keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk || keyCode == '*')
	{
		menuBar->SetPopup(ETrue);
		DrawNow();
		return;
	}
	else if(keyCode==m_pGlobal->g_nKeySelect)
	{
		if(m_nCurrent >= 0 && m_nCurrent < m_vContacters.Count())
		{
			m_vContacters[m_nCurrent].iSelected = !m_vContacters[m_nCurrent].iSelected;
			DrawNow();	
		}
		return;
	}
	else if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
		TInt nItem = m_vContacters.Count();
		if (nItem>1)
		{
			TInt nOldCurrentItem = m_nCurrent;
			if (keyCode==m_pGlobal->g_nKeyUp)
			{
				m_nCurrent--;
				if (m_nCurrent<0)
					m_nCurrent = nItem-1;
			}
			else
			{
				m_nCurrent++;
				if (m_nCurrent>=nItem)
					m_nCurrent = 0;
			}
			if (nOldCurrentItem!=m_nCurrent)
			{
				if (m_bShowScroll)
					m_sbVert->SetPos(m_nCurrent);
				DrawNow();
			}
		}
		return;
	}
	else
		CBlock::OnKeyDown(keyCode);
}

void CBlockContacterList::ChangeScrollPos(TInt nPos)
{
	if (m_nCurrent!=nPos)
	{
		m_nCurrent = nPos;
		DrawNow();
	}
}

void CBlockContacterList::InitPopMenu(const TInt& /*nType*/)
{
	menuBar->m_subMenu.ResetAndDestroy();

	menuBar->Append(CPopMenu::NewL( KMenuIdRecmd,LOC_BUF_STR_RECOMMENDFRIEND));
	menuBar->Append(CPopMenu::NewL( KMenuIdSelAll,LOC_BUF_STR_SELALL));
	menuBar->Append(CPopMenu::NewL( KMenuIdSelNone,LOC_BUF_STR_SELNONE));
}

TBool CBlockContacterList::OnMenuEvent(TInt menuId)
{
	switch(menuId)
	{
	case KMenuIdRecmd:
		{
			if(m_vContacters.Count()>0)
			{
				CBlockRecommend* blockRecommend = (CBlockRecommend*)m_pBlockBack;
				for(TInt i=0; i<m_vContacters.Count(); i++)
				{
					if(m_vContacters[i].iSelected)
						blockRecommend->AddContacter(m_vContacters[i].iPhone);
				}
				blockRecommend->m_bSocketed = EFalse;
				if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
					eStockUI->GetCurView()->iContainer->DelBlock();
			}
			else
			{
				if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
					eStockUI->GetCurView()->iContainer->DelBlockNoSocket();
			}
		}
		break;
	case KMenuIdSelAll:
		{
			for(TInt i=0; i<m_vContacters.Count(); i++)
				m_vContacters[i].iSelected = ETrue;
			DrawNow();
		}
		break;
	case KMenuIdSelNone:
		{
			for(TInt i=0; i<m_vContacters.Count(); i++)
				m_vContacters[i].iSelected = EFalse;
			DrawNow();
		}
		break;
	default:
		break;
	}
	return EFalse;
}

void CBlockContacterList::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = aPt.iY/m_nLineHeight-1;
	if (nPos>=0&&nPos<m_nLinesPerPage)
	{
		nPos += m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;
		if (nPos<m_vContacters.Count())
		{
			if (m_nCurrent!=nPos)
			{
				m_nCurrent = nPos;
				if (m_bShowScroll)
					m_sbVert->SetPos(m_nCurrent);
			}
			else
			{
				OnKeyDown(m_pGlobal->g_nKeySelect);
			}
			DrawNow();
		}
	}
}
	
void CBlockContacterList::InitCntList()
{
	CContactDatabase* database = CContactDatabase::OpenL();
	const CContactIdArray*	cntIds = database->SortedItemsL();

	for(TInt i=0; i<cntIds->Count(); i++)
	{
		TContacter contacter;

		CContactItem* contact = database->OpenContactL((*cntIds)[i]);
		CleanupStack::PushL(contact);
		CContactItemFieldSet& fieldSet = contact->CardFields();
		
		TInt fieldCount = 0;
		for(TInt j=0; j<fieldSet.Count(); j++)	//\x{fffd}\x{fffd}\x{fffd}\x{fffd}j\x{03f5}\x{fffd}\x{02f8}\x{fffd}\x{fffd}\x{05b6}\x{fffd}
		{
			CContactItemField& field = fieldSet[j];
			if (field.ContentType().ContainsFieldType(KUidContactFieldGivenName)) //\x{fffd}\x{fffd}
			{
				contacter.iName.Append(field.TextStorage()->Text());
				fieldCount++;
			}
			else if (field.ContentType().ContainsFieldType(KUidContactFieldFamilyName)) //\x{fffd}\x{fffd}
			{
				contacter.iName.Insert(0,field.TextStorage()->Text());
				fieldCount++;
			}
			else if (field.ContentType().ContainsFieldType(KUidContactFieldPhoneNumber))//\x{fffd}\x{05bb}\x{fffd}
			{
				if(contacter.iPhone.Length() < 1)
					contacter.iPhone.Copy(field.TextStorage()->Text());
				fieldCount++;
			}
			if(fieldCount >=3)
				break;
		}
		database->CloseContactL((*cntIds)[i]);
		CleanupStack::PopAndDestroy(contact);
		m_vContacters.Append(contacter);
	}

	delete database;
}

void CBlockContacterList::SizeChanged()
{
	CBlock::SizeChanged();
	SetScrollBar(m_nCurrent, m_vContacters.Count());
}
